package org.thirdHero.db.template;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.thirdHero.core.constant.CommonConstant;
import org.thirdHero.core.constant.DomainConstant;
import org.thirdHero.core.domain.Domain;
import org.thirdHero.core.domain.DomainOptions;
import org.thirdHero.core.domain.DomainType;
import org.thirdHero.db.constant.SystemConstant;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenerateVue {
    private static String demoName = "SysRedPackage";
    private static String targetPath = "/Volumes/code 1/fresh-admin/litemall-admin/src/views/shop/";
    private static String templateDirectory = "/Volumes/code 1/fresh-service/travel-db/src/main/resources/template/";
    private static String packageName = "org.thirdHero.admin.web";
    // 模板目录
    // 模板名称
    private static String templateFile = "domainVue.vue.btl";
    // 模板生成后存放目录
    // 模板生成后新文件名
    private static String fileName = demoName+".vue";
    //需要进行查询的参数列表
    private static List<String> params = new ArrayList<String>();
    //需要动态查询的属性列表
    private static List<String> properties = new ArrayList<>();

    private static List<DomainType> domainTypeList = new ArrayList<>();

    private static List<DomainType> domainSel = new ArrayList<>();
    private static  List<DomainType> mustInput = new ArrayList<>();

    private static int haveUpload = 2;//是否有上传组件1、有 2、无



    static {
        properties.add("name");


        DomainType domain1 = new DomainType();
        domain1.setChinaName("主键");
        domain1.setName("id");
        domain1.setType(DomainConstant.INPUT);

        DomainType domain2 = new DomainType();
        domain2.setChinaName("创建时间");
        domain2.setName("createDate");
        domain2.setType(DomainConstant.DATE);

        DomainType domain3 = new DomainType();
        domain3.setChinaName("修改时间");
        domain3.setName("modifyDate");
        domain3.setType(DomainConstant.INPUT);

        DomainType domain4 = new DomainType();
        domain4.setChinaName("红包金额");
        domain4.setName("money");
        domain4.setType(DomainConstant.INPUT);

        DomainType domain5 = new DomainType();
        domain5.setChinaName("起始金额");
        domain5.setName("limitMoney");
        domain5.setType(DomainConstant.INPUT);

        DomainType domain6 = new DomainType();
        domain6.setChinaName("有效起始日期");
        domain6.setName("effectStartDate");
        domain6.setType(DomainConstant.DATE);

        DomainType domain7 = new DomainType();
        domain7.setChinaName("有效截止日期");
        domain7.setName("effectEndDate");
        domain7.setType(DomainConstant.INPUT);

        DomainType domain8 = new DomainType();
        domain8.setChinaName("数量");
        domain8.setName("numbers");
        domain8.setType(DomainConstant.INPUT);

        DomainType domain9 = new DomainType();
        domain9.setChinaName("红包名称");
        domain9.setName("name");
        domain9.setType(DomainConstant.INPUT);

        DomainType domain10 = new DomainType();
        domain10.setChinaName("可使用商品类别");
        domain10.setName("categoryId");
        domain10.setType(DomainConstant.INPUT);

        DomainType domain11 = new DomainType();
        domain11.setChinaName("店主id");
        domain11.setName("shopLegalId");
        domain11.setType(DomainConstant.INPUT);

        DomainType domain12 = new DomainType();
        domain12.setChinaName("描述信息");
        domain12.setName("desc");
        domain12.setType(DomainConstant.INPUT);
        // domain3.setOptions(initEnableOptions());
      //  domain16.setOptions(initOptions2());

        domainTypeList.add(domain1);
        domainTypeList.add(domain2);
        domainTypeList.add(domain3);
        domainTypeList.add(domain4);
        domainTypeList.add(domain5);
        domainTypeList.add(domain6);
        domainTypeList.add(domain7);
        domainTypeList.add(domain8);
        domainTypeList.add(domain9);
        domainTypeList.add(domain10);
        domainTypeList.add(domain11);
        domainTypeList.add(domain12);



        domainSel.add(domain3);
        domainSel.add(domain8);
        domainSel.add(domain9);



    }



    public static void main(String[] args) throws IOException {
        // 参数值
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", "String");
        map.put("demoName", demoName);
        map.put("packageName",packageName);
        map.put("properties",properties);
        map.put("domains",domainTypeList);
        map.put("domainSels",domainSel);
        map.put("haveUpload",haveUpload);
        map.put("domainSize",domainTypeList.size()/2);
        map.put("mustInput",mustInput);

        // 创建文件夹
        new File(targetPath).mkdirs();
        File nFile = new File(targetPath +"/"+ fileName);
        if (nFile.exists()) {
            throw new RuntimeException("File \'"+fileName+"\' already exists");
        }

        // 生成目标文件
        Writer writer = null;
        try {
            writer = new FileWriter(nFile);

            Template template = getConfiguration(templateDirectory).getTemplate(templateFile, "UTF-8");
            template.process(map, writer);

        } catch (TemplateException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            writer.close();
        }
    }

    private static Configuration getConfiguration(String templateDirectory) {

        Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
        try {
            configuration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
            configuration.setDirectoryForTemplateLoading(new File(templateDirectory));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return configuration;
    }

}
